
package com.gao.security.autoconfig.verificationcode.properties;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import static com.gao.security.autoconfig.verificationcode.properties.ValidateCodeProperties.VALIDATE_CODE_PREFIX;

/**
 * <p>
 * 验证码配置文件
 * </p>
 *
 * @author GaoShuangWen
 */

@ConfigurationProperties(prefix = VALIDATE_CODE_PREFIX)
@Data
public class ValidateCodeProperties implements InitializingBean {
    /**
     * 安全配置前缀
     */
    protected static final String VALIDATE_CODE_PREFIX = "api.validate.code";

    @NestedConfigurationProperty
    private ImgRegisterUrlInfo img = new ImgRegisterUrlInfo();

    @NestedConfigurationProperty
    private SmsRegisterUrlInfo sms=new SmsRegisterUrlInfo();

    @NestedConfigurationProperty
    private EmailRegisterUrlInfo email=new EmailRegisterUrlInfo();

    @Override
    public void afterPropertiesSet() throws Exception {
        // 判断发送地址不能相同
        RegisterUrlInfo img = this.img;
        RegisterUrlInfo sms = this.sms;
        RegisterUrlInfo email = this.email;
        if (StringUtils.equalsAny(img.getSendUrl(), sms.getSendUrl(), email.getSendUrl())) {
            throw new BeanInitializationException("验证码的发送地址必须唯一");
        }
        if (StringUtils.equalsAny(img.getLoginUrl(), sms.getLoginUrl(), email.getLoginUrl())) {
            throw new BeanInitializationException("验证码的登录地址必须唯一");
        }
    }

    @Data
    public static class ImgRegisterUrlInfo implements RegisterUrlInfo {
        /**
         * 发送验证码url
         */
        private String sendUrl = "/img/code";

        /**
         * 登录的url
         */
        private String loginUrl = "/img/login";

    }

    @Data
    public static class SmsRegisterUrlInfo implements RegisterUrlInfo {
        /**
         * 发送验证码url
         */
        private String sendUrl = "/sms/code";

        /**
         * 登录的url
         */
        private String loginUrl = "/sms/login";

    }

    @Data
    public static class EmailRegisterUrlInfo implements RegisterUrlInfo{
        /**
         * 发送验证码url
         */
        private String sendUrl = "/email/code";

        /**
         * 登录的url
         */
        private String loginUrl = "/email/login";

    }


}
